package com.two.zelin.dao.impl;
//实现层
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.two.zelin.controller.DataBaseUtil;
import com.two.zelin.dao.orderDao;
import com.two.zelin.model.order;

public class orderDaoImpl implements orderDao {

	/**查询所有订单  select * from daifukuana limit 0,10*/
	@Override
	public List<order> selectAll() {
		List<order> list = new ArrayList<order>();
		
		Connection conn = DataBaseUtil.getConnection();
		String sql = "select * from daifukuana where flag = 1";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while(rs.next()) {
				order user = new order();
				user.setFukuan(rs.getString("fukuan"));
				user.setSpname(rs.getString("spname"));
				user.setSpid(rs.getString("spid"));
				user.setJage(rs.getInt("jage"));
				user.setZhekou(rs.getString("zhekou"));
				user.setShouhuo(rs.getString("shouhuo"));
				user.setShouhuoren(rs.getString("shouhuoren"));
				user.setLianxidianhua(rs.getString("lianxidianhua"));
				list.add(user);
				user = null; // 将user对象置为空便于快速回收内存
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	//	/**查询根据订单名来查询*/
//	@Override
//	public order selectByname(String spname) {
//				//获取数据库的连接
//				Connection conn = DataBaseUtil.getConnection();
//				//准备sql
//				String sql = "select * from dingdan where spname=? ";
//				//获取声明对象
//				PreparedStatement ps = null;
//				ResultSet rs =null;
//				Order order = null;
//				try {
//				 ps = conn.prepareStatement(sql);
//				 //补全sql
//				 ps.setString(1, username);
//				 //执行并返回结果
//				 rs =ps.executeQuery();
//				 //解析结果集
//				 if(rs.next()) { 
//					 //如果有数据则转发到首页
//					 order user = new order();
//						user.setFukuan(rs.getString("fukuan"));
//						user.setSpname(rs.getString("spname"));
//						user.setSpid(rs.getString("spid"));
//						user.setJage(rs.getInt("jage"));
//						user.setZhekou(rs.getString("zhekou"));
//						user.setShouhuo(rs.getString("shouhuo"));
//						user.setShouhuoren(rs.getString("shouhuoren"));
//						user.setLianxidianhua(rs.getString("lianxidianhua"));
//				 }
//				}catch (SQLException e) {
//					e.printStackTrace();
//				}finally {
//					//关闭资源
//					DataBaseUtil.closeResource(conn, ps, rs);
//				}
//			
//		return order;
//	}
	
	/**根据订单id来查询*/
	@Override
	public order selectById(String id) {
		Connection conn = DataBaseUtil.getConnection();
		String sql = "select * from daifukuana where id = ?";
		PreparedStatement ps = null;
		ResultSet rs= null;
		System.out.println("Dao - spname: " + id);
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, id);
			rs = ps.executeQuery();
			if(rs.next()) {
				order o = new order();
				o.setFukuan(rs.getString("fukuan"));
				o.setSpname(rs.getString("spname"));
				o.setSpid(rs.getString("spid"));
				o.setJage(rs.getInt("jage"));
				o.setZhekou(rs.getString("zhekou"));
				o.setShouhuo(rs.getString("shouhuo"));
				o.setShouhuoren(rs.getString("shouhuoren"));
				o.setLianxidianhua(rs.getString("lianxidianhua"));
				return o;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	
	}
	
	/**添加订单*/
	@Override
	public int addorder(String fukuan,String spname,String spid, int jage, String zhekou, String shouhuo) {
		//获取数据库的连接
				Connection conn = DataBaseUtil.getConnection();
				//准备sql
				String sql = "insert into `daifukuana`(`fukuan`,`spname`,`spid`,`jage`,`zhekou`,`shouhuo`) values(?,?,?,?,?,?,1)";
				System.out.println(spname);
				System.out.println(spid);
				//获取声明对象
				PreparedStatement ps = null;
				ResultSet rs =null;
				try {
				 ps = conn.prepareStatement(sql);
				 //补全sql
				 ps.setString(1, fukuan);
				 ps.setString(2, spname);
				 ps.setString(3, spid);
				 ps.setLong(4, jage);
				 ps.setString(5, zhekou);
				 ps.setString(6, shouhuo);
				 ps.setInt(6, 1);
				 //执行sql并更新数据库
				 int n = ps.executeUpdate();
				 return n;
				}catch (SQLException e) {
					e.printStackTrace();
				}finally {
					//关闭资源
					DataBaseUtil.closeResource(conn, ps, null);
					
				}
		return 0;
		/***/
	}
	
	/**更新订单信息*/
	@Override
	public int updateorder(int id,String spid,String fukuan,String spname, int jage, String zhekou, String shouhuo) {
		Connection conn = DataBaseUtil.getConnection();
		String sql = "UPDATE `spid` SET `fukuan`=?, `spname`=?,"
				+ " `jage`=?, `zhekou`=?, `shouhuo`=? WHERE (`id`=?)";
		PreparedStatement ps = null;
		int n = 0;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, spid);
			ps.setString(2, fukuan);
			ps.setString(3, spname);
			ps.setInt(4, jage);
			ps.setString(5, zhekou);
			ps.setString(6, shouhuo);
			ps.setInt(7, id);
			n = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DataBaseUtil.closeResource(conn, ps, null);
		}
		return n;
	
	}
	
	/**删除订单*/
	@Override
	public int deleteorder(String id) {
		Connection conn = DataBaseUtil.getConnection();
		String sql = "update dingdan set flag=0 where id=?";
		PreparedStatement ps = null;
		int n = 0;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, id);
			n = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DataBaseUtil.closeResource(conn, ps, null);
		}
		return n;
	}
	
}
